<?php
namespace app\api\service;
use think\Controller;
use think\Db;
use think\Exception;
use think\Session;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/5/26 0026
 * Time: 15:40
 */

class Member extends Controller {

    /**
     * 获取用户中心首页数据
     * @param $param
     * @return mixed
     */
    public static function get_member_info($param) {
        try {
            // 用户数据
            $member  = model('Member')->get(['id' => $param['member_id']]);
            if (!$member) {
                throw new \Exception('用户不存在');
            }

            // 返回数据
            $data['nickname'] = $member->memberRoleInfo->nickname; // 昵称
            $data['headimgurl'] = $member->memberRoleInfo->headimgurl ? get_img_url($member->memberRoleInfo->headimgurl) : '';  // 头像
            $data['money'] = number_format($member->money, 2);  // 余额
            $data['integral'] = $member->integral;  // 积分

            // 是否签到
            $registration = Registration::check_registration($param);
            $data['registration'] = $registration['code'];

            // 是否实名认证
            $data['certification_status'] = $member->certification ? $member->certification->status : 0;

            // 店铺
            $data['shop_id'] = $member->shop ? $member->shop->id : 0;

            return api_return(200, $data);
        } catch (\Exception $e) {
            return api_return(0, [], $e->getMessage());
        }
    }

    /**
     * 获取个人信息
     * @param $param
     * @return array
     */
    public static function get_member_role_info($param) {

        try {
            // 用户数据
            $member = model('Member')->get(['id' => $param['member_id']]);

            $data = [
                'headimgurl' => $member->memberRoleInfo->headimgurl ? get_img_url($member->memberRoleInfo->headimgurl) : '',  // 头像
                'nickname' => $member->memberRoleInfo->nickname,
                'sex' => $member->memberRoleInfo->sex,
                'birthday' => $member->memberRoleInfo->birthday
            ];

            return api_return(200, $data);
        } catch (\Exception $e) {
            Db::rollback();
            return api_return(0, [], $e->getMessage());
        }
    }

    /**
     * 保存个人信息数据
     * @param $param
     * @return array
     */
    public static function save_member_role_info($param) {
        try {
            $value = $param['value'];
            if ($param['field'] == 'birthday') {
                $value = strtotime($value);
            }
            if ($param['field'] == 'headimgurl') {
                $value .= '-200';  //  七牛云图片需要加上后缀
                model('RongMemberInfo')->save(['portraitUri' => $value], ['member_id' => $param['member_id']]);
            }
            model('MemberRoleInfo')->save([$param['field'] => $value], ['member_id' => $param['member_id']]);

            // 修改昵称和头像之后  融云信息表也同时修改一下
            if ($param['field'] == 'nickname') {
                model('RongMemberInfo')->save(['name' => $value], ['member_id' => $param['member_id']]);
            }

            return api_return(200, []);
        } catch (\Exception $e) {
            return api_return(0, [], $e->getMessage());
        }
    }
}